﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BookExchange.DAO;
using BookExchange.ODataService;

namespace BookExchange.Business
{
    public abstract class BaseBusiness<T>
    {
        public abstract void Insert(T obj);
        public virtual void Update(T obj)
        {
            this.DB.UpdateObject(obj);
            this.DB.SaveChanges();

        }
        public virtual void Delete(T obj)
        {
            this.DB.DeleteObject(obj);
            this.DB.SaveChanges();
        }

        protected IQueryable<T> GetQuery(IQueryable<T> query, int skip = 0,int take = 0,
            System.Linq.Expressions.Expression<Func<T, bool>> predicate = null)
        {
            IQueryable<T> queryResult;

            queryResult = predicate != null ? query.Where(predicate) : query;

            queryResult = skip > 0 ? queryResult.Skip(skip) : queryResult;

            queryResult = take > 0 ? queryResult.Take(take) : queryResult;

            return queryResult;
        }

        protected BookExchaneContext DB
        {
            get
            {
                return BaseDao.database;
            }
            set
            {
                BaseDao.database = value;
            }
        }
    }

}
